const fs = require("fs")
const OSS = require('ali-oss')
const axios = require('axios')
class FileListPlugin {
    apply(compiler) {
        const env_hezi = process.env.VUE_APP_TITLE === 'build-hezi'
        console.log("开始执行插件......")
        compiler.plugin('compile', function () {
            console.log("webpack 编译器开始编译......")
        })
        compiler.plugin('done', function () {
            async function FileManagerPlugin() {
                const copyCss = await fs.copyFileSync("./dist/css/app-pc.css", "./dist/css/app-android.css")
                const copyJs = await fs.copyFileSync("./dist/js/app-test.js", "./dist/js/app-official.js")
                let css = await fs.readFileSync("./dist/css/app-android.css", "utf-8")
                let js = await fs.readFileSync("./dist/js/app-test.js", "utf-8")
                const replaceString = "https://hz-huibenxia.oss-cn-shenzhen.aliyuncs.com/Static-library/"
                while (css.indexOf(replaceString) !== -1) {
                    css = css.replace(replaceString, "https://load/.nomedia/Static-library/")
                }
                js = js.replace("https://android-back-test.huibenxia.com/", "https://android-back.huibenxia.com/")
                js = js.replace("https://activity-back-test.huibenxia.com/", "https://activity-back.huibenxia.com/")
                const writeCss = fs.writeFileSync("./dist/css/app-android.css", css)
                const writeJs = fs.writeFileSync("./dist/js/app-official.js", js)
                console.log("文件生成成功......")
            }
            FileManagerPlugin()
            async function putOss() {
                const client = new OSS({
                    region: 'oss-cn-shenzhen',
                    accessKeyId: 'LTAIAXJl14rN6DzO',
                    accessKeySecret: 'QIV5oFL6q6dwwyw4UjCECMVDBetSP1',
                    bucket: env_hezi ? 'hz-huibenxia' : 'hz-huibenxia-test'
                })
                const urls = [
                    "js/app-test.js",
                    "js/app-official.js",
                    "js/chunk-vendors.js",
                    "css/app-android.css",
                    "css/app-pc.css",
                    "css/chunk-vendors.css"
                ]
                for (let i = 0, len = urls.length; i < len; i++) {
                    const putUrl = urls[i].substring(urls[i].lastIndexOf("/") + 1, urls[i].length)
                    await client.put("static-resource/" + putUrl, 'dist/' + urls[i])
                }
                console.log("上传OSS成功......")
            }
            putOss().then(() => {
                const axiosUrl = env_hezi ?"https://android-back.huibenxia.com/front/clear-static-resource-cache.do"
                    : "http://android-back-test.huibenxia.com/front/clear-static-resource-cache.do"
                axios.post(axiosUrl).then(() => {
                    console.log("已通知刷新缓存......")
                })
                console.log("打包完成......")
            })
        })
    }
}

module.exports = FileListPlugin;
